package com.chen.controller;

import com.chen.entity.Student;
import com.chen.entity.vo.Result;
import com.chen.entity.vo.ResultCodeEnum;
import com.chen.entity.vo.StudentVo;
import com.chen.mapper.StudentMapper;
import com.chen.serivce.StudentService;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("api/student")
@RequiredArgsConstructor //用来自动生成一个包含所有 final 字段（和带 @NonNull 注解字段）的 构造函数。
public class StudentController {

    private final StudentService studentService;

    //    @Autowired
//    public StudentController(StudentService studentService) {
//        this.studentService = studentService;
//    }


    /**
     * 根据学生名字查学分
     *
     */
    @GetMapping("/getCreditByName/{name}")
    public Result getCreditByName(@PathVariable String name){
        List<Map<String, Object>> mapList = studentService.getCreditByName(name);
        return Result.build(mapList,ResultCodeEnum.SUCCESS);
    }
    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/selectByPage/{pageNum}/{pageSize}")
    public Result<PageInfo<Student>> selectByPage(@PathVariable Integer pageNum,
                                                  @PathVariable Integer pageSize) {
        PageInfo<Student> pageInfo = studentService.selectByPage(pageNum, pageSize);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        studentService.removeById(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 新增
     *
     * @param student
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Student student) {
        studentService.save(student);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 更新
     *
     * @param
     * @return
     */
    @PutMapping("/updateById")
    public Result updateById(@RequestBody Student student) {
        studentService.updateById(student);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * num为题号
     * @param num
     * @return
     */
    @GetMapping("getByNum/{num}")
    public Result getByNum(@PathVariable Integer num){
        if (num==1){
            List<Student> studentList= studentService.getByOne();
            return Result.build(studentList,ResultCodeEnum.SUCCESS);
        }else {
            List<StudentVo> studentvoList= studentService.getBytwo();
            return Result.build(studentvoList,ResultCodeEnum.SUCCESS);
        }
    }

}
